Tag Info

New answers tagged

1

Arbitrarily. The threads waiting are pushed into a container and on notify_one it will pick one and remove it. Usually the one most easily found in the structure. If you specify that the condition is fair then it's the oldest one. Often selected by keeping an ordered ring buffer that can grow as needed.


3

It is operating system specific (or hardware specific if you are coding some kernel-like software on the bare metal like an OS kernel -see osdev.org-, in freestanding C). Stricto sensu you probably cannot write a game in purely standard hosted C99 (without using any external library, except the C standard library using only the few functions standardized in ...


0

The simplest solution is to use interrupts and timers. The interrupts indicate: User I/O inputs Timer expiration Two separate programs could communicate via two pipes or msgsnd() or semaphores or mutexes or signals. (I think there is no need for multiple programs.) If you write the program so that everything is interrupt driven, along with a ...


0

1.1) you can't think in term of one Thread by user. Depending of machine configuration but you could load thousands of threads but it will not scale and lose a lot of time in Thread context switch. Better think NIO Netty like framework with few incoming and outcoming Thread pool executor that perform incoming messages under milli second execution. 1.2) ...


-2

I'd say that this kind of question is very hard to answer, because it asks for the best way to do something completely stupid. My brain just doesn't work that way. It cannot find solutions for stupid questions. My brain would immediately say that under these conditions, using multiple threads is pointless, so I would use a single thread. I'd then ask them ...


4

strtok is not thread-safe because it saves its state in static memory between calls. If two threads make concurrent or interleaved calls to strtok, they will get erroneous results. The strtok_r variant returns a pointer to the caller to use for future invocations on the same string. There are many other C and POSIX functions that are not safe to use from ...


0

If you want to ensure that your variables are consistent it is advisable to provide thread synchronization mechanisms to prevent crashes and race conditions I think you just hit what they were after. In any language you will have parts of it that are not thread safe and need synchronization mechanisms to prevent issues (look at the .net List for a ...


2

4 threads are nothing, especially since these background tasks that you described are mostly doing nothing but waiting for something to happen. A modern server can easily have hundreds of such threads simultaneously running, precisely because the word "running" here is mostly a figure of speech: they are not really running, they are just waiting. I open ...


5

I hear threads are expensive. While that isn't an issue yet, I don't want to get into the habit of spawning a new thread for everything if there's a better way. Threads have overhead. The longer a thread lives, the less of a concern the overhead is. For example, let us assume a thread has a ton of overhead and requires 100ms to spin up. However, the ...


0

Java synchronisation uses a recursive lock for the instance of the synchronised method. It is as if each instance had a recursive lock, and @synchronized would lock that recursive lock (JVM uses a more clever method obviously). Therefore: Any number of threads can use @synchronized for different objects. One thread can use @synchronized for the same ...


4

If you read carefully the documentation of synchronized you will find that it is explicitly stated that once a thread has acquired a lock, it is allowed to re-acquire it as many times as it pleases. According to the Java Language Specification: Section 8.4.3.6. "synchronized Methods" says that the synchronized keyword acquires a monitor. See: ...


2

In Java, synchronized locks are re-entrant. Or in other words if your thread already holds the lock on an object it doesn't have to wait on itself.


1

Boost may be cutting edge when it is first released. But it (boost) should also be considered a testing ground for new ideas. The thread ideas introduced in boost were a good idea at the time and the lessons learned helped the standardization committee when they added threads to the standard. But now that threads are part of the standard you should be ...


1

You can't (realistically) prove thread safety exhaustively for a language like Java, but there are ways to detect some bugs. The official behaviour of your Java program when run on several threads is defined by Chapter 17 of the JLS. The only way to prove that a program is thread safe is by applying all those rules in a theoretical way, which in most ...


1

You have not told us what kind of multi-threading you do, but I presume you do multi-threading of the locking kind, (you use the synchronized keyword of java,) otherwise you probably would not be asking the question. As testing has gained ground in the software engineering discipline during the last decade or so, multi-threading of the locking kind has ...


4

Thread safety means correctness in the face of the exponentially many possible interactions between concurrent processes. Many concurrency-related bugs occur only in a small minority of possible interleavings (but still occur in production because "One in a million is next Tuesday"). Additionally, testing in general is better at finding bugs than assuring ...


2

Demonstrating thread-safety means proving that every possible interleaving of the instructions in two opcode streams leads to the correct result. Therefore, in order to test this property, you must be able to interleave those instructions as desired. In practice, this means that you must have some kind of hook within code that is normally run as a unit. ...



Top 50 recent answers are included