I am trying to write my first multi-threaded program in C under Linux. I already have a program playing with setting and resetting bits in a big buffer, now I just want to make it faster - as fast as possible without writing everything in assembler.
For the single-threaded program, I defined my own macros for the bit manipulation (they kind of look big and ugly, but they work):
#define CELL_SIZE (64)
#define getBit(bitIdx, arr) ( arr[ (bitIdx) / CELL_SIZE ] & ( (1uL << (CELL_SIZE - 1)) >> (bitIdx) % CELL_SIZE))
#define resetBit(bitIdx, arr) ( arr[ (bitIdx) / CELL_SIZE ] &= ~( (1uL << (CELL_SIZE - 1)) >> (bitIdx) % CELL_SIZE))
Obviously, those operations are anything BUT atomic.
I did some research and I found several answers. Some answers are about how to write my own functions in assembler (which I want to avoid).
Other sites (this or this) seem to talk exactly about what I need - except that I have no idea how to use those interfaces / macros.
I tried the following includes, but they do not work.
#include <linux/bitmap.h>
#include <asm/bitops.h>
So, basically, I ask how can I use what is already implemented in order to get my work done? Which header(s) should I include?
I do not mind if the interfaces are not in the kernel, as long as the job gets done.
Note: each thread would (re)set exactly one bit at one time - I do not plan any VOODOO complicated stuff.
A side question would be: which one would be faster (overall, written in pseudo-code):
reset_bit();
or
if ( bit_is_set() )
reset_bit();
It is quite often that the reset_bit() operation is not necessary, as the bit is already reset. Same question about setting a set bit.