Understanding pthreads a little more in C - c

So I only very recently heard about these pthreads and my understanding of them is very limited so far but I just wanted to know if it would be able to do what I want before I get real into learning about them.
I have written a program that generates two output pulses from a micro-controller which happen with different frequencies, periods and duty cycles. At the moment the functions to output the pulses are happening in a loop and it works well because the timings I am using are multiples of each other so stopping one while not interrupting the other is not too much hassle.
However I want it be a lot more dynamic so I can change the duty cycles or periods easily without having to make some complicated loop specific for those timings... Below shows a quick sketch of what I am trying to achieve and I hope you can understand it...
So basically my question is, is something like this possible with pthreads in C, ie do they run simultaneously so one could be pulsing on and off while the one is waiting for a delay to finish?
If not is there anything that I could use for this instead?

In general, it's not worth using threads for such functionality on a uC. The cost of extra stacks etc. for such limited operations is not worth it, tempting it might be from a simplicity POV.
A hardware timer, interrupt and a delta-queue of events is probably the best you could do.

Related

About Dijkstra omp

Recently I've download a source code from internet of the OpenMP Dijkstra.
But I found that the parallel time will always larger than when it is run by one thread (whatever I use two, four or eight threads.)
Since I'm new to OpenMP I really want to figure out what happens.
The is due to the overheard of setting up the threads. The execution time of the work itself is theoretically the same, but the system has to set up the threads that manage the work (even if there's only one). For little work, or for only one thread, this overhead time makes your time-to-solution slower than the serial time-to-solution.
Alternatively, if you see the time increasing dramatically as you increase the thread-count, you could only be using 1 core on your computer and tricking it into thinking its 2,4,8, etc threads.
Finally, it's possible that the way you're implementing dijkstra's method is largely serial. But without looking at your code it would be too hard to say.

Implementing general timeouts

I'm porting some code from C# to C. In the C# code there are three timers that fire if particular events take too long and they set flags that are checked next time a thread runs a bit of housekeeping.
The C is pure C, not C++, and will eventually be used on both Linux and in embedded targets, so I can't use any OS oriented stuff- simple soft timers. I started off using just an "enabled" flag and a due time for each timer, in ms, and when I call the housekeeping function I'll pass the current ms timer value to it. Then I started thinking of the wraparound issue and decided I wanted the start time as well, so if the present time isn't between the start time and the due time I know it's expired. And I want the default duration to be there as well, so it ends up being worth making a structure to represent a timer. And then making functions that work with pointers to these structures. And then it started me thinking I may be reinventing the wheel.
I don't see anything in the standard libraries that looks like this. Am I missing something? Is this just something that's just easier to do than to look for? :)
Ta for commenting. That's the way I went, just wanted to make sure I wasn't wasting work. Yeah embedded stuff tends to have a timer interrupt, but three is probably asking a bit much and adds hardware dependencies- I'm just passing the current ms timer value to my code and then it doesn't have to care about where that value's coming from. – Craig Graham

Launching background threads in C with a single statement

I've read quite a bit about the Go language. There's a lot I didn't like about it - enough that I don't want to use it in day-to-day life. However, there is one bit about the language that I really do like: goroutines.
I was thinking of ways of implementing it in C. So far, the best I can find on the internet is
#define go if (!fork()) for(;;exit(0))
That way, you can prefix function calls with go, so that
go printf("Hello, world!\n");
runs in a different thread, as well as:
go { printf("Hello, world!\n"); foo(); bar(); baz(); }
But, of course, fork() has speed issues. (On my box, it takes 7 times as long to fork() as it is to printf(), benchmarked using the rdtsc x86 instruction and running a few times to eliminate the possibility of switching between cores or being scheduled out.)
So, my question is, is there a better way of implementing this, so that it's faster?
Goroutines are mostly a fancy word for threads, with some extra functionality for inter-thread communication and such.
I am guessing the part you are interested in is the ability to succintly run a section of code in a separate thread. Unfortunately there isn't a simple way to do this in C - you would have to write a function that enclosed the code you wanted to run in a the background, and use a macro or function that accepted that function and did the necessary magic using pthread_create() or similar.
Unless someone comes up with a clever way to use macros to create a function on-the-fly? Anyone?
Keep in mind that in all but the most basic threaded application you will need some sort of synchronization, which will make things much less simple.
Take a look at OpenMP. It allows threads to be spawned for code blocks and loop iterations with relatively simple #pragma directives. It has been around for over a decade, and is already available in many compilers (including gcc).
Starting work in a thread should be faster than fork(), but the performance improvement may be obscured by behind-the-scenes thread-pool initialization overhead in simple applications that don't manage many threads.

Reliable to use cudaEvent_t to measure CPU time?

I have a simple kernel without using multiple events, and i want to create a CPU version of it which i've done and measure the difference between them. I don't know if events are strictly created for CUDA, but i guess my example is simple enough and does not contain anything to be ok to do that. Opinions?
If you are measuring time on the CPU, nothing is better than High performance counters E.g for java, you can measure time in the nano second range. Events are generally used for the GPU as the start and stop event are added to the GPU queue, not the CPU one.

Threading OR multiple flow of control in Pacman

I am planning to write a Pacman game in C language, right from scratch. The most basic challenge that I am facing is how to maintain multiple flows of control at the same time.
I mean how does the Pacman move, the ghosts move, the score being updated -- all at the same time. In general it is very common for all games. Is any kind of threading involved here?
If so can anyone please tell as to how to make your program do many things at the same time (it will be helpful if you tell for C language).
Thanks in advance
One of the fundamental principle in real time game development is the game tick. It represents a small unit of time for things to happen in. So you might have a tick every 0.100 seconds. The smaller the tick, the finer control you have.
You can think of them as really fast turns with a time limit on them. If you don't do anything on that turn you forfeit the turn.
I think it's pretty unlikely that the original version of Pac-Man was multithreaded in the sense we use the term today. It was more likely implemented as a simple loop with some kind of interrupt support. You can do the same to implement rudimentary multithreading - write your program in a while (1) or for (;;) loop, and set up a timer to interrupt your loop at regular intervals to perform the screen updates.

Resources