Process or threads synchronization in Linux


New Member
Jun 6, 2023
Reaction score
I am creating a program which uses multiple threads, but I am not able to synchronize such that a function threaded by an thread1 always execute first and no another threads able to run change shared resources. I heard about mutex locks and semaphore but I have no idea how to use. my code still in progress so I am not able to shared it instead I want to understand changes in basic code which is pretty much similar.
also which one is better semaphore or mutex for this case.

void *fun1();
void *fun2();
int shared=1;
int main()
    pthread_t thread1, thread2;
    pthread_create(&thread1, NULL, fun1, NULL);
    pthread_create(&thread2, NULL, fun2, NULL);
    pthread_join(thread1, NULL);
    printf("Final value of shared is %d\n",shared);

void *fun1()
    int x;

void *fun2()
    int y;

Multi-threading is used to allow the scheduler to efficiently allocate resources to different threads of your program. This is so that if one thread blocks waiting on a resource, your program can still make progress. The whole idea is that there is no forced ordering of execution. If you're trying to force one thread to execute before another, the obvious question is why are you using multiple threads?

Mutexes and semaphore are used to lock a particular shared resource like an area of memory or a variable used by both threads. If you want to learn more about how they work, I highly recommend the book "The Linux Programming Interface". It's an excellent reference.
I saw this yesterday and decided to sleep on it because I did not know where to start or what to say. One issue is that @gf2rm lacks a basic knowledge of multi-threaded programming, but is asking for a code solution to a specific problem. The other issue is that the problem has not been defined well because the OP appears to need more understanding of multi-threaded programming. :-(

Because the original post question is not clear enough, I did not know whether to offer one of many possible code solutions or suggestions to learn more about multi-threaded programming. @MattWinter chose the latter, and I like his response. Just to help, this is what I think the real requirements are:
  • Thread 1 always launches and begins execution first. Note: Thread 1 could be interrupted or suspended after the first instruction and you have no control over that.
  • The threads share global resources (variables, structures, etc.) and gf2rm wants to make sure that only one thread can change a given resource at a time.
    • -> What is missing is whether that exclusivity runs until the thread completes, or only for the instruction that changes the global resource. The prototype code in the original post make it seem like the full thread.
The answer to Matt's question of "Why multi-threaded?" may be that this is prototype code to examine the basic multi-threading principles, and the real program requirements are unknown to us. That was one reason why a good response with a code sample would be a challenge.

Learning the basics of multi-threaded programming is not difficult. The difficulty is choosing a design that does what you want, runs efficiently, and cannot deadlock. The OP is here because they know that there may be more than one way to do that.
-> First: learn the multi-threading concepts and building blocks and what they do. It should not take long. Next, learn how they can be used in different ways to achieve what you want. Try out some sample code and you will be on your way ... Practice debugging - maybe write a deadlock to see what it looks like in the debugger.