pthreads | POSIX threads for Windows | File Utils library
kandi X-RAY | pthreads Summary
kandi X-RAY | pthreads Summary
Pthreads-win32 is an Open Source Software implementation of the Threads component of the POSIX 1003.1c 1995 Standard (or later) for Microsoft’s Win32 environment. Some functions from POSIX 1003.1b are also supported including semaphores. Other related functions include the set of read-write lock functions. The library also supports some of the functionality of the Open Group’s Single Unix specification, version 2, namely mutex types, plus some common and pthreads-win32 specific non-portable routines (see README.NONPORTABLE). See the file "ANNOUNCE" for more information including standards conformance details and the list of supported and unsupported routines.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of pthreads
pthreads Key Features
pthreads Examples and Code Snippets
Community Discussions
Trending Discussions on pthreads
QUESTION
i have been writing some function that needs to multile threaded in C , pthreads seemed like a no brainer.But now that i have written it out it is giving my segment faults all the time , with a bunch of printf() statements and commenting out part of codes i figured out that pthread_create is causing these , any help would be appreciated! As far as i can see me passing in the parameters to pthread_create seems perfectly fine.
code :
...ANSWER
Answered 2021-Jun-08 at 17:14The code has two bugs.
One is that the thread that runs main
can terminate, destroying the stack on which opcount
exists. This can happen before the threads have actually had a chance to terminate. You don't wait for them to terminate. (And they won't terminate because of the second bug.)
The second bug is that the threads you try to cancel don't have any way to be cancelled. They don't check if they've been cancelled. They don't enable async cancellation.
What's likely happening is your code crashes when the threads access objects on the first thread's stack after the first thread has terminated and succeeds if, by luck, the process terminates before that happens because returning from main
terminates the process.
QUESTION
So I've got a weird issue that I don't quite understand why it is happening. In md4checker, I launch n pthreads that get and check an MD4 hash. In md4.c, I generate an MD4 hash. If I set n threads to 1, it works flawlessly. It generates the MD4 hash with perfect accuracy (I ran it in a loop for 1,000,000 tries and not a single time did it fail). However, when I run this same code with n threads as 2 (or higher) it fails a lot and randomly.
The md4.c file is derivative of another I found online but I tweaked it a little because the original md4.c had a memory leak (and running 50,000,000+ hashes made that leak fill up 16GB of RAM in about 15 minutes). If it was just a matter of it not working, I'd know where to start but I'm genuinely at a loss as to where and why multiple threads corrupt each other here.
edit: If I add usleep(100) to the worker thread in md4checker.c, it cuts the failure rate to 10% of what it normally does.
md4checker.c (works when running just one):
...ANSWER
Answered 2021-Jun-02 at 00:03So why the random number of fails?
The MD4 code presented is not thread safe, and you are adding a bit of thread-unsafety of your own.
Observe in particular variables A
, B
, C
, and D
in file md4.c
. These are declared at file scope and without the _Thread_local
qualifier, so they have static storage duration and are shared by all threads in the process. These are modified during the computation, so you have data races involving all of these. The resulting behavior is undefined, and it shouldn't be hard to imagine how it might mess things up if multiple threads were clobbering the values that each other had written in those variables.
As for your own code, with each call to runprocs()
, the main thread and each new one created all share the same struct data
object, which the threads read and modify and the main thread reads, all without synchronization. This also causes undefined behavior, though it looks like this could be rescued by engaging a mutex or other synchronization mechanism.
Additionally, the MD4 code appears to be deterministic -- given the same input, it will always (if run single-threaded to avoid undefined behavior) produce the same output. It is therefore unclear what you seek to accomplish by running it in multiple threads on the same input.
Also, the while(!d.done)
loop is pointless and poor form. You should be joining each thread via pthread_join()
to clean up its resources after it, and since that has the (primary) effect of waiting for the thread to terminate, you don't need to also roll your own wait for termination.
QUESTION
I'm trying to convert a .ts file with this output to mkv:
...ANSWER
Answered 2021-May-02 at 08:14Try to run with the -ss
flag.
QUESTION
I'm trying solve a multiple producer-consumer problem using pthreads and semaphore but it always sticks at the last consume and halt. It will have NO_ITEMS of items and suppose buffer have size BUFFER_SIZE
This is my current code below.
...ANSWER
Answered 2021-May-23 at 13:27Your code has a logic problem. Suppose NO_ITEMS
is 100, and 99 have so far been consumed. Let two consumer threads arrive at the top of the while
loop at that point, and suppose that both read count
as 99 (but see below), and therefore enter the body of the loop. Both consumers will block on sem_wait()
, but there is at most one more item to be produced, so the producer will increment the semaphore at most once more, leaving at least one of the consumers blocked indefinitely.
Moreover, your thread_consumer()
function contains a data race, leaving your program's behavior undefined. Specifically, the read of shared variable count
in the while
condition is not properly synchronized. Although one cannot reliably predict how UB will manifest (else it would not be "undefined"), it is fairly common for unsynchronized accesses to manifest apparent failures of one thread to see shared-variable updates of other threads. Such a failure mode would explain your particular observed behavior all by itself.
Very likely, a correct fix to this synchronization problem would also fix the logic problem.
SolutionsThere are multiple possible solutions. Here are some promising ones:
Semaphores are not a particularly comfortable fit for the problem. You need a mutex anyway, and its usual counterpart for signaling is a condition variable. I would convert the two semaphores to two (or maybe just one) ordinary integer variable, and use a standard mutex + CV pattern in both producer and consumer. That would include adding mutex protection for the read of
count
in the consumer.On the other hand, if you are obligated to use semaphores, then you could
- add appropriate mutex protection for the consumers' read of
count
- be sure to retain the consumers' test for whether they can actually consume an item after successfully decrementing the semaphore
- have the main program post twice (number of consumers - 1 times) to
fillCount
after joining the producer thread but before attempting to join the consumers. This will unblock any consumers that thought they would be able to consume an item but end up still waiting after the last item is consumed by another consumer.
- add appropriate mutex protection for the consumers' read of
Or you could employ a hybrid: retain the
emptyCount
semaphore to limit the number of items waiting at any given time (instead of switching to a CV for that purpose), but switch to a mutex + CV pattern for managing the consumers.
QUESTION
I am writing a tool for tracking jumps(JAL/JALR
) instructions on riscv instructions. I am using https://github.com/illumine/disk-benchmark/blob/master/src/disk-benchmark.c
disk benchmark to test my tool. The tool is tracking jumps on binary files when virtual functions and pthreads are not used but have difficulties to track when pthreads are used. Here is the objdump of binary file on riscv:
ANSWER
Answered 2021-May-11 at 02:44We can trace the jumps on binary
file only if we don't have any indirect jumps and if the trace is not involved in library functions. In my case I couldn't trace since the program had a lot of pthreads and indirect jumps.
QUESTION
I'm helping a student with their homework, which is a basic threading exercise. Unfortunately, though they're required to use C++11, they're forbidden from using std::thread
. I don't see the rationale, but it's not my homework.
Here's the class:
...ANSWER
Answered 2021-May-06 at 07:03except for how much fluff you have to do to get the old and the new to play nice.
Well, in the STL, that's essentially what the std::thread
is actually doing. If you create a thread and force it to cause a stack unwinding, and if you look at said stack, you'll see a lot of weird pointer arithmetic happening with this
and pthread_create
(or CreateThread
on Windows).
That being said, it's not unusual in any way to use a static
function of a class that then calls a private
member of that class on an object instance, even with the std::thread
, it really just depends on what you need those functions to do.
does using a static member method as the start_routine argument to
pthread_create
have any undesirable side effects?
No. At least not from the perspective of functionality; that is, creating a thread on a static member won't cause any UB or crashes directly just because you are using a static
function.
You do have to account for the fact that your operating on a static member function, but that's no different from having to account for constructors/destructors or any function of the language itself. Since this is a homework assignment, it's likely the professor is trying to teach "how things work" less than "how to use C++11".
Would it just be better, in this case, to make
vaccine_count
public and makecount_vaccines()
a global function?
Yes and no. Having vaccine_count
as a private member then means that count_vaccines
must be a friend or static function, and given that vaccine_count
seems like an "important" data point that you wouldn't want a "user of the code" inadvertently setting, it's probably better to keep it private.
You could add getters and setters, but that might complicate the code unnecessarily.
You could also just make it a public variable if you trust the users of the code to protect that variable (unlikely), and you could also just make count_vaccines
a free/global function, but then you need to have the function after the class declaration. And if the class is a complex class (maybe has templates or some other C++ notion), then it can really complicate the code in how you operate on the class.
So yes, it could go that way, but the professor is likely trying to teach the idea of what a static function is, how threads operate on the class and how pointers work within the constructs of this exercise, among other things.
If you have a static member variable, all objects access that variable.
That's not what static
means in this context. The static
keyword in C++ simply means that you do not need an object reference to call that code. So a static
member variable can be accessed, not just by any object, but by any code, take this example:
QUESTION
When I run ffprobe
, I get the standard metadata as below:
ANSWER
Answered 2021-Apr-29 at 15:13You may use shlex.split
or put the arguments to FFprobe in a list.
In Windows OS, you can use sp.run(f'ffprobe {video}'
...
In Linux and Mac, Python tries to execute the command as file name with spaces.
For example: 'ffprobe vid.mp4'
is considered a single executable command (file name with space).
You may use an arguments list:
sp.run(['ffprobe', f'{video}']
...
Or use shlex.split
for splitting the shell command to a list:
sp.run(shlex.split(f'ffprobe {video}'))
...
For simple parsing FFprobe output in Python:
- Execute ffprobe with
-of json
argument, and get the output in JSON format. - Convert the output string to dictionary using
json.loads
.
Here is a code sample that reads the output of FFprobe into a dictionary:
QUESTION
I am using the following command on several video streams to pipe them into my TVHeadend server.
...ANSWER
Answered 2021-Apr-27 at 14:59Change from
QUESTION
Using ffmpeg I add a video overlay successfully over an origin video (origin has audio, overlay doesn't). However the audio of the origin video doesn't appear in the result video.
...ANSWER
Answered 2021-Apr-26 at 23:42Tell it which audio you want with -map
:
QUESTION
We have a little Nodejs app, which starts a stream process, with a child_process.spawn
. On the client-side, we have an HTML5-canvas element, which records the video data new MediaRecorder(canvas.captureStream(30), config)
, then this client sends its data to our Nodejs server over a WebSocket connection. We using FFmpeg for video encoding and decoding, then we send the data to our 3-rd party service (MUX), which accepts the stream and broadcasts them. Sadly the process continuously loses its fps, and after in general 1 minute, stops with an interesting error code. (when we save the video result locally instead of streaming via rtmps
, it works perfectly.
*The whole system is in docker.
The error:
...ANSWER
Answered 2021-Apr-07 at 06:11Im found another FFmpeg config that works perfectly.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install pthreads
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page