Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
How do Initialize threads in SDL. Ive read the documentation and i dont understand how to pass variables, whats really happening in the thread. What does Mutex mean, and locking ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Apr 2003
    Location
    TEXAS
    Posts
    314

    Threads for SDL


    How do Initialize threads in SDL. Ive read the documentation and i dont understand how to pass variables, whats really happening in the thread. What does Mutex mean, and locking variables. How do I keep multiple threads from accessing the same variable.
    The computer made me do it!! Slackware and SUSE too Gig\'em WHOOOOP!!
    \"God put me on this earth to accomplish a certain amount of tasks, At the rate I\'m going I will never die.\" (I don\'t know)

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Well, I can't tell you how SDL handles threads, since I don't use it, but I can tell you how to lock variables. That's what mutexes are good for. Mutex stands for mutual exclusion device, and it provides atomical operations to make sure that only one thread at a time does a certain thing.
    Again, I can't tell how to do this in SDL, but this is how normal POSIX pthread mutexes work:
    You create a variable of type pthread_mutex_t, and initalize it with pthread_mutex_init(&mutex). Then you can use pthread_mutex_lock(&var) to lock the mutex. When a mutex is locked, and another thread tries to lock it, the lock request is queued and the thread is blocked until the first thread unlocks the mutex with pthread_mutex_unlock(&var). Here's a simple example:
    Code:
    #include <pthread.h>
    
    struct sensitive
    &#123;
        int data;
        pthread_mutex_t mutex
    &#125;;
    
    struct sensitive *sd;
    
    void *thread1&#40;void *data&#41;
    &#123;
        while&#40;1&#41;
        &#123;
            ...
            pthread_mutex_lock&#40;&sd->mutex&#41;;
            sd->data++;
            pthread_mutex_unlock&#40;&sd->mutex&#41;;
            ...
        &#125;
    &#125;
    
    void *thread2&#40;void *data&#41;
    &#123;
        while&#40;1&#41;
        &#123;
            ...
            pthread_mutex_lock&#40;&sd->mutex&#41;;
            sd->data = 0;
            pthread_mutex_unlock&#40;&sd->mutex&#41;;
            ...
        &#125;
    &#125;
    
    int main&#40;void&#41;
    &#123;
        pthread_t t1, t2;
        
        sd = malloc&#40;sizeof&#40;*sd&#41;&#41;;
        pthread_mutex_init&#40;&sd->mutex&#41;;
        pthread_create&#40;&t1, NULL, thread1, NULL&#41;;
        pthread_create&#40;&t2, NULL, thread2, NULL&#41;;
        pause&#40;&#41;;
    &#125;
    But that's just for POSIX pthreads. I don't know how it works in SDL.

    You might also want to look up semaphores. It's all documented in the texinfo for glibc.

  3. #3
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319

    emacs and C

    Dolda,

    Can you use emacs so that the curly brace lines up properly after using while, if, for and other statements? In my emacs, I can only do it if it's used the following way:
    Code:
    while&#40; 1 &#41; &#123;
      ... 
    &#125;
    as opposed to your code:
    Code:
    while&#40;1&#41;
    &#123;
      ...
    &#125;
    The best things in life are free.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Lines up properly? Do you mean so that indents properly? In that case, that works very well in my version of emacs. Which emacs and cc-mode version are you using? To find out what version of cc-mode you have, run these emacs strokes:
    M-x load-library RET cc-mode RET C-: c-version RET
    Also, check so that you're using the new cc-mode version of c-mode, and not the old c-mode one. Type C-h f c-mode RET and check on the first line which module it is in. It should, obviously, be cc-mode (ie. it should say something like "c-mode is an interactive autoloaded Lisp function in `cc-mode'.").

  6. #5
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    Well,

    It indents properly but look at the difference with the curly brace in the examples above. If I try to do the second one, this is what I get:
    Code:
    while&#40;1&#41;
      &#123;
         ...
      &#125;
    The best things in life are free.

  7. #6
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    Version was 5.28
    The best things in life are free.

  8. #7
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Ah, that. That's just because it's set to gnu indenting mode. Type C-c . when in c-mode to change indenting style. I use the "stroustrup" mode. To set a default mode, add this to your ~/.emacs:
    Code:
    &#40;add-hook 'c-mode-hook &#40;lambda &#40;&#41; &#40;c-set-style "stroustrup"&#41;&#41;&#41;

  9. #8
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    Is there a way to find out which indentation style mode is running?
    The best things in life are free.

  10. #9
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    By the way, adding that line to .emacs doesn't do anything nor does it give me an error when emacs starts. Any ideas?
    The best things in life are free.

  11. #10
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    Actually, it works for only C and not C++. As I'm looking through emacs, I cannot seem to find such a set mode to change it for C++ or Java. Any ideas?
    The best things in life are free.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •