as a qualifier on any static or global variable. This is a widely supported POSIX standard but there are other ways of doing the job and in particular C11 introduced its own way that works if you are using C11 threading.
As an example, let’s try two simple counting threads sharing a global counter variable:
You can see that the counter variable is global and both threads increment it 5000 times. It might appear that the result of printing the two return values would be 5000 each time. However, as both threads access the variable in an uncontrolled way sometimes things go wrong as both threads access the count variable at the same time - take the same value it contains, increment it and store the same incremented value back. What should have been an increment by two becomes an increment by one. If you run the program you will get results in the 6000 region indicating the simultaneous updates aren't that rare.
Now if you change the declaration of count to:
__thread int counter;
and rerun the program you will see that returnval1 prints 5000 and returnva2 prints 5000 also. Each thread has its own copy of the global count variable and there is no sharing and hence no interaction. Of course the variable is still global and any other function executed on the same thread will have access to its value.
A thread local variable is safe to use within a thread but how do we correct the counter program with a shared global so that we always get the right answer - the most obvious answer is use a lock but there is another.
Included in chapter but not in this extract:
Atomic Operations and Locks
Mutex
Condition Variables
First Thread to Finish
Scheduling
Deadline Scheduling
Summary
Multi-tasking is becoming increasingly common, even on small machines.
The original way of creating new processes is to use a fork which creates a new copy of the currently running process. Today it is more common to use threads, which are more efficient and closer bound than a fork.
Although C11 has a standard for threading, the original Pthreads library is still the most common way of implement threading.
Pthreads supports joinable and detached threads. A joinable thread can return a result to a thread that waits on its completion, whereas a detached thread cannot do this.
To avoid simultaneous access to resources you need to use locking. The mutex is one of the most basic locking devices.
A more complex locking device is the condition variable, which can be used to synchronize threads that can wait on the condition variable.
Threads and processes need to be scheduled by the operating system. Linux has a real time scheduling policy called FIFO, which is worth using, but the slightly less commonly used earliest deadline scheduling (EDS) is better.
Thomas Eugene Kurtz, the co-founder of the BASIC programming language, has died at the age of 96. BASIC, which was developed for the purpose of education, popularized computer programming making it ac [ ... ]
JetBrains has improved its IDEs with features to suggest the logical structure of code, to streamline the debugging experience for Kubernetes applications, and provide comprehensive cluster-wide Kuber [ ... ]